(function ($) {
	var LOGIN_URL = 'login';

	var showKapkeyCode = 403001;//超过出错次数
	var showErrorKakeyCode = 403002;//验证码错误
	var showAccountErrorCode = 403003;//账号错误
	var showPasswordErrorCode = 403006;//密码错误


	var Form = function () {
		this.$form = $("#login-form");
		this.$btn = $('#login');
		this.$imgKey = $('#imgKey');
		this.$account = $('#account');
		this.$pwd = $('#password');
		this.$remember = $('#rememberMe');
		this.reloginFlag = $('#reloginFlag').val();
		this.rkey = 'rememberLogin';
		this.ukey = 'accountLogin';
		this.pkey = 'passwordLogin';
	};

	$.extend(Form.prototype, {
		init: function () {
			this.initParameter();
			this.initValidate();
			this.initFormEvent();
			util.initPlaceholder(this.$account, '账户名');
			util.initPlaceholder(this.$pwd, '密码');
			// this.initRemember();
			this.$imgKey.click();
			/*
			if ($.browser.msie && $.browser.version == '6.0') {
				this.$pwd.focus();
				this.$pwd.blur();
			}
			*/
		},
		initParameter: function () {
			var appuri = util.getParameter("appuri");
			var useruri = util.getParameter("useruri");
			var service = util.getParameter("service");
			var sid = util.getParameter("sid");

			if (appuri != null) {
				$('#appuri').val(appuri);
			}
			if (useruri != null) {
				$('#useruri').val(useruri);
			}
			if (service != null) {
				$('#service').val(service);
			}
			if (sid != null) {
				$('#sid').val(sid);
			}
		},
		initRemember: function () {
			if (util.getCookie(this.rkey) == 'true') {
				$('#remember').prop('checked', true);
				this.$account.val(util.getCookie(this.ukey));
				this.$pwd.val(util.getCookie(this.pkey));
				util.hidePHTip(this.$account);
				util.hidePHTip(this.$pwd);
			}
			this.$remember.mzCheckBox({
				click: function (e, event) {
				}
			});
		},
		showKakey: function (code, mes) {
			if (code == showKapkeyCode) {
				$('#kapkeyWrap').show();
				util.addTips("password", mes);
				this.initResize(900);
				return true;
			}
			if (code == showErrorKakeyCode) {
				$('#kapkeyWrap').show();
				util.addTips("kapkey", mes);
				this.initResize(900);
				return true;
			}
			return false;
		},
		showErrorTips: function (code, mes) {
			if (code == showAccountErrorCode) {
				util.addTips("account", mes);
				return true;
			}
			if (code == showPasswordErrorCode) {
				util.addTips("password", mes);
				return true;
			}
			return false;
		},
		initResize: function (h) {
			global.resizer.setProperty('minH', h);
			$(document.body).css('min-height', h);
		},
		initInput: function ($input, info) {
			util.initPlaceholder($input, info, 'emptyInput');
		},
		initFormEvent: function () {
			var me = this;
			this.$btn.click(function () {
				me.$form.submit();
			});
			var timeout = null;
			this.$form.bind("keypress", function (e) {
				if (e.keyCode == 13) {
					me.$btn.click();
				}
			});
			this.$imgKey.click(function () {
				//$(this).attr('src', '/kaptcha.jpg?t='+(new Date().getTime()));
				$(this).attr('src', '/captcha.png?t=' + (new Date().getTime()));
			});
		},
		initValidate: function () {
			var _this = this;
			_this.$form.ajaxForm({
				dataType: 'json',
				beforeSubmit: function (data, from, opts) {
					var name = _this.$pwd.attr('name'),
						passwd = _this.$pwd.val(),
						npk = (util.getCookie('npk') || '').split(':'),
						key = new RSAKey;

					if (!npk.length) {
						return false;
					}

					key.setPublic(b64tohex(npk[0]), b64tohex(npk[1]));
					passwd = hex2b64(key.encrypt(passwd));

					$.each(data, function (i, el) {
						if (name == el.name) {
							el.value = passwd;
							return false;
						}
					});
				},
				success: function (data) {
					window.location.href = data.href;
				}
			});
			util.rule.account = util.rule.accountOrPhone;
			util.message.account = util.message.accountOrPhone;
			util.rule.account.nameOrD11 = undefined;
			var rules = util.createRule({account: null, kapkey: null});
			var messages = util.createMes({account: null, kapkey: null});
			rules.password = {
				required: true
			};
			messages.password = {
				required: "密码不能为空"
			};
			this.$form.validate($.extend(util.validate, {
				/*
				 submitHandler: function(){
				 if(_this.reloginFlag){
				 loginUrl = reloginUrl;
				 }
				 _this.$form.ajaxSubmit({
				 type: "post",
				 url: loginUrl,
				 dataType: "json",
				 success: function(result){
				 result = util.getData(result, false, function(mes, code){
				 if(code != '200' && _this.$imgKey.is(':visible')){
				 _this.$imgKey.click();
				 }
				 if(_this.showKakey(code,mes)){
				 return;
				 }
				 if(_this.showErrorTips(code,mes)){
				 return;
				 }
				 });
				 if(result == null){
				 _this.$pwd.val('');
				 return;
				 };
				 if(_this.$remember.prop('checked')){
				 util.setCookie(_this.rkey, 'true');
				 util.setCookie(_this.ukey, _this.$account.val());
				 util.setCookie(_this.pkey, _this.$pwd.val());
				 }else{
				 util.deleteCookie(_this.rkey);
				 util.deleteCookie(_this.ukey);
				 util.deleteCookie(_this.pkey);
				 }
				 location.href = result;
				 },
				 error: function(result){
				 nAlert("网络错误！","提示");
				 }
				 });
				 },
				 */
				rules: rules,
				messages: messages
			}));
		}
	});

	$(function () {
		var form = new Form();
		form.init();
	});
})(jQuery);
